﻿@model IEnumerable<_PLATFORM.DATA.PRODUCT>
@using _PLATFORM.DATA.Custom_Classes;
@{
    ViewBag.Title = "Index";
}



@{
    var categories = new List<List<ProductCategory>>();
            
    if (ViewBag.ProductType == "Component") 
    {
        categories = ViewBag.ProductCategories as List<List<ProductCategory>>;
    }
    
    var totalCategories = 0;
    var idx = 0;
    var ctr = 0;
    foreach (var category in categories)
    {
        if (category.Count > totalCategories)
        {
            idx = ctr;
        }
        totalCategories = (category.Count > totalCategories) ? category.Count : totalCategories;
        ctr++;
    }
                }
<div id="subNav">
    <ul>
    	<li>@Html.ActionLink("Systems Management", "IndexList", "TruckingSystem")</li>
        @if(ViewBag.ProductType == "Component"){
            <li class="select">@Html.ActionLink("Component Management", "Index", "Component", new { productType = "Component" }, null)</li>
            <li>@Html.ActionLink("Spare Part Management", "Index", "Component", new { productType = "SparePart" }, null)</li>            
        }
        else{
            <li>@Html.ActionLink("Component Management", "Index", "Component", new { productType = "Component" }, null)</li>
            <li class="select">@Html.ActionLink("Spare Part Management", "Index", "Component", new { productType = "SparePart" }, null)</li>
        }
    </ul>
</div>
@if (ViewBag.ProductType == "Component")
{
    <br /><h2>Component Management</h2>
}
else { 
    <br /><h2>Spare Part Management</h2>
}

<div id="subNav2">
    @if (ViewBag.ProductType == "Component")
    {
        <ul>
    	    <li  class="select">@Html.ActionLink("Current Components", "Index", "Component", new { productType = "Component" }, null)</li>
            <li>@Html.ActionLink("Create New Component", "Create", "Component", new { productType = "Component" }, null)<img src="../../Assets/images/add_icon.gif" alt="Add" /></li>
            <li>@Html.ActionLink("Manage Component Selections", "Index", "SelectionOption", new { productType = "Component" }, null)</li>
        </ul>
    }
    else
    {
        <ul>
    	    <li  class="select">@Html.ActionLink("Current Spare Parts", "Index", "Component", new { productType = "SparePart" }, null)</li>
            <li>@Html.ActionLink("Create New Spare Parts", "Create", "Component", new { productType = "SparePart" }, null)<img src="../../Assets/images/add_icon.gif" alt="Add" /></li>
            <li>@Html.ActionLink("Manage Spare Parts Selections", "Index", "SelectionOption", new { productType = "SparePart" }, null)</li>
        </ul>
    }
</div>

    <div id="lowerContainer">
		<div id="lowerContainerInner">
            <table id="tableMgmt" class="display">
                <thead>
                    <tr>
                        <th>
                            <b>Name</b>
                        </th>
                        <th>
                            <b>Cost</b>
                        </th>
                        <th>
                            <b>Price</b>
                        </th>
                        <th>
                            <b>Supplier</b>
                        </th>
                        <th>
                            <b>Part Number</b>
                        </th>
                        <th>
                            <b>Lead Time</b>
                        </th>
                        <th>
                            <b>Inventory</b>
                        </th>
                        <th>
                            <b>Image</b>
                        </th>
                        @{
                            if (ViewBag.ProductType == "Component")
                            {
                                if (categories.Count != 0)
                                {
                                    for (var i = 0; i < totalCategories; i++)
                                    {
                                        var title = categories[idx][i].Name;
                                        <th>
                                            <b>
                                                @title
                                            </b>
                                        </th>
                                    }
                                }
                            }
                        }
                    </tr>
                </thead>
                @{
                    var itemCounter = 0;
                }
                <tbody>
                    @foreach (var item in Model)
                    {
    
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.NAME)

                                @{
                           var pdfName = item.PDF;
                           var pdfPath = Url.Content("~/Content/User/PDFs/" + pdfName);
                                }
                                @if (!String.IsNullOrEmpty(pdfName))
                                { 
                                    <a href="@pdfPath" target="_blank">
                                        @Html.Image(Url.Content("~/Assets/images/pdf_icon.gif"), item.NAME + " pdf.", new { style = "width:15px;height:15px;" })
                                    </a>
                                }
                                else
                                { 
                                    @Html.DisplayText("No PDF")
                                }
                                <br /><br />
                                @Html.ActionLink("Edit", "Edit", new { id = item.PRODUCT_ID }, new { @class = "button default btnExtraSmall" })
                                @Html.ActionLink("Delete", "Delete", new { id = item.PRODUCT_ID }, new { @class = "button default btnExtraSmall" })
                                @Html.ActionLink("Duplicate", "Duplicate", new { id = item.PRODUCT_ID }, new { @class = "button default btnExtraSmall" }) 
                    
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.COST)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.PRICE)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.SUPPLIER_NAME)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.PART_NUMBER)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.LEAD_TIME)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.INVENTORY)
                            </td>
                            <td>
                                @{
                           var imageName = item.IMAGE;
                           var path = Url.Content("~/Content/User/Images/" + imageName);
                                }
                                @Html.Image(path, item.NAME + " image.", new { style = "width:100px;height:75px;" })
                            </td>
                            @{
                           if (ViewBag.ProductType == "Component")
                           {
                               ctr = 0;
                               foreach (var category in categories[itemCounter])
                               { 
                                        <td>
                                            <ul>
                                                @{
                                   if (category.Items != null && category.Items.Count > 0)
                                   {
                                       foreach (var asset in category.Items)
                                       {
                                           var assetName = asset.Name;
                                                                 <li>
                                                                    @assetName
                                                                 </li>   
                                       }
                                   }
                                                }
                                    
                                            </ul>
                                        </td>
                                   ctr++;
                               }
                               if (ctr < totalCategories)
                               {
                                   do
                                   {
                                            <td></td>
                                       ctr++;
                                   } while (ctr < totalCategories);
                               }
                           }
                            }
        
                        </tr>
                           {
                               itemCounter++;
                           }

                    }
                </tbody>
                <tfoot>
                    <tr>
                        <th>
                            <b>Name</b>
                        </th>
                        <th>
                            <b>Cost</b>
                        </th>
                        <th>
                            <b>Price</b>
                        </th>
                        <th>
                            <b>Supplier</b>
                        </th>
                        <th>
                            <b>Part Number</b>
                        </th>
                        <th>
                            <b>Lead Time</b>
                        </th>
                        <th>
                            <b>Inventory</b>
                        </th>
                        <th>
                            <b>Image</b>
                        </th>
                        @{
                            if (ViewBag.ProductType == "Component")
                            {
                                if (categories.Count != 0)
                                {
                                    for (var i = 0; i < totalCategories; i++)
                                    {
                                        var title = categories[idx][i].Name;
                                        <th>
                                            <b>
                                                @title
                                            </b>
                                        </th>
                                    }
                                }
                            }
                        }
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>